package com.ayoka.database;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.ayoka.bean.MusicFileBean;
import com.jcp.baselibrary.utils.MyLogger;
import com.jcp.baselibrary.utils.UIUtils;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by Administrator on 2015/7/9.
 */
public class DBManager {
    private static DbHelper helper;
    private static DBManager instance;

    public static DBManager getInstance() {
        if (instance == null) {
            synchronized (DBManager.class) {
                instance = new DBManager();
            }
        }
        return instance;
    }

    private DBManager() {
        helper = new DbHelper(UIUtils.getContext());
    }

    /**
     * 查询所有的文件
     */
    public List<MusicFileBean> queryMusics() {
        SQLiteDatabase database = helper.getReadableDatabase();
        database.beginTransaction();
        List<MusicFileBean> datas = new ArrayList<>();
        String[] columns = {MusicEntity.COLUMN_NAME, MusicEntity.COLUMN_DURATION, MusicEntity.COLUMN_SIZE};
        Cursor cursor = database.query(true, MusicEntity.TABLE_NAME, columns, null, null, null, null, null, null);
        for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
            String name = cursor.getString(cursor.getColumnIndex(MusicEntity.COLUMN_NAME));
            MusicFileBean musicFileBean = new MusicFileBean(name);
            musicFileBean.duration = cursor.getInt(cursor.getColumnIndex(MusicEntity.COLUMN_DURATION));
            musicFileBean.size = cursor.getLong(cursor.getColumnIndex(MusicEntity.COLUMN_SIZE));
            datas.add(musicFileBean);
        }
        database.setTransactionSuccessful();
        database.endTransaction();
        cursor.close();
        database.close();
        MyLogger.d("DBManager", "查询所有成功");
        return datas;
    }

    /**
     * 查询音乐
     */
    public MusicFileBean queryMusic(String name) {
        SQLiteDatabase database = helper.getReadableDatabase();
        database.beginTransaction();
        MusicFileBean musicFileBean = null;
        String[] columns = {MusicEntity.COLUMN_NAME, MusicEntity.COLUMN_DURATION, MusicEntity.COLUMN_SIZE};
        Cursor cursor = database.query(true, MusicEntity.TABLE_NAME, columns, MusicEntity.COLUMN_NAME + "=?", new String[]{name}, null, null, null, null);
        if (cursor.moveToNext()) {
            musicFileBean = new MusicFileBean(name);
            musicFileBean.duration = cursor.getInt(cursor.getColumnIndex(MusicEntity.COLUMN_DURATION));
            musicFileBean.size = cursor.getLong(cursor.getColumnIndex(MusicEntity.COLUMN_SIZE));
        }
        database.setTransactionSuccessful();
        database.endTransaction();
        cursor.close();
        database.close();
        MyLogger.d("DBManager", "查询成功");
        return musicFileBean;
    }

    /**
     * 保存音乐
     */
    public void saveMusic(MusicFileBean musicBean) {
        SQLiteDatabase database = helper.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(MusicEntity.COLUMN_NAME, musicBean.name);
        contentValues.put(MusicEntity.COLUMN_DURATION, musicBean.duration);
        contentValues.put(MusicEntity.COLUMN_SIZE, musicBean.size);
        long result = database.replace(MusicEntity.TABLE_NAME, null, contentValues);
        MyLogger.d("DBManager", "保存成功" + result);
    }

    /**
     * 更新音乐
     */
    public void updateMusic(MusicFileBean musicBean) {
        SQLiteDatabase db = helper.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(MusicEntity.COLUMN_NAME, musicBean.name);
        contentValues.put(MusicEntity.COLUMN_DURATION, musicBean.duration);
        contentValues.put(MusicEntity.COLUMN_SIZE, musicBean.size);
        db.update(MusicEntity.TABLE_NAME, contentValues, MusicEntity.COLUMN_NAME + "=?", new String[]{musicBean.name});
        MyLogger.d("DBManager", "更新成功");
    }
}
